

package com.yz.service.impl;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.yz.common.entity.*;
import com.yz.common.utils.R;
import com.yz.dao.DiscoverDao;
import com.yz.dao.RuinDao;
import com.yz.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service("discoverService")
public class DiscoverServiceImpl extends ServiceImpl<DiscoverDao,DiscoverEntity> implements DiscoverService {
    @Autowired
    CheckService checkService;
    @Autowired
    CollectionService collectionService;
    @Autowired
    TestService testService;
    @Autowired
    CameraService cameraService;
    @Autowired
    ProjectService projectService;
    @Autowired
    DrawingService drawingService;
    @Autowired
    VedioService vedioService;

    @Override
    public long selectId() {
        return this.baseMapper.selectId();
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void postDiscover(DiscoverForm t,UserEntity user) {
        DiscoverEntity data = t.getDiscoverEntity();
        CheckEntity check = new CheckEntity();
        check.setCreateAt(data.getUpdateAt());
        check.setCreateUser(data.getCreateUser());
        check.setName(projectService.selectById(data.getProjectId()).getName());
        check.setNameForm(contant.type_name_form[4]);
        check.setProjectId(data.getProjectId());
        check.setStatus(1);
        check.setType(5);
        check.setTypeForm(101);
        long id = 0;
        if(data.getId()==0){
            this.baseMapper.insert(data);
            id = this.baseMapper.selectId();
            check.setDataId(id);
            check.setNumber(contant.type_all_form[4]+contant.type_detail_form[4]+id);
            checkService.insert(check);
        }else{
            id = data.getId();
            check.setDataId(id);
            check.setNumber(contant.type_all_form[4]+contant.type_detail_form[4]+id);
            checkService.update(check,new EntityWrapper<CheckEntity>().eq("number",contant.type_all_form[4]+contant.type_detail_form[4]+id));
            testService.delete(new EntityWrapper<TestEntity>().eq("discover_id",id));
            collectionService.delete(new EntityWrapper<ConnectionsEntity>().eq("discover_id",id));
            this.baseMapper.update(data,new EntityWrapper<DiscoverEntity>().eq("id",id));
        }
        //插入或者更新附表数据
        List<ConnectionsEntity> collections = t.getConnectionsEntities();
        for (int i = 0; i < collections.size(); i++) {
            ConnectionsEntity item = collections.get(i);
            item.setDiscoverId(id);
           collectionService.insert(item);
        }
        List<TestEntity> tests = t.getTestEntities();
        for (int i = 0; i < tests.size(); i++) {
            TestEntity item1 = tests.get(i);
            item1.setDiscoverId(id);
            testService.insert(item1);
        }
        //图片放入数据库
        if(t.getSchematic()!=null && t.getSchematic().size()>0){//示意图
            cameraService.delete(new EntityWrapper<CameraEntity>().eq("filed_name","4").eq("data_id",id).eq("type_form",101));
            for (int i = 0; i <t.getSchematic().size() ; i++) {
                CameraEntity item = t.getSchematic().get(i);
                item.setPosterId(user.getUserId());
                item.setPostTime(data.getCreateAt());
                item.setCreateUser(user.getUsername());
                item.setProjectId(data.getProjectId());
                item.setTypeWhere(2);
                item.setTypeForm(101);
                item.setDataId(id);
                item.setFiledName("4");//示意图
                item.setNumber(contant.type_all_form[4]+contant.type_detail_form[4]+id);
                cameraService.insert(item);
            }
        }
        if(t.getDrawingNumber()!=null&& t.getDrawingNumber().size()>0){//绘图
            drawingService.delete(new EntityWrapper<DrawingEntity>().eq("data_id",id).eq("type_form",101));
            for (int i = 0; i <t.getDrawingNumber().size() ; i++) {
                DrawingEntity item1 = t.getDrawingNumber().get(i);
                item1.setPosterId(user.getUserId());
                item1.setPostTime(data.getCreateAt());
                item1.setCreateUser(user.getUsername());
                item1.setProjectId(data.getProjectId());
                item1.setTypeWhere(2);
                item1.setTypeForm(101);
                item1.setDataId(id);
                item1.setNumber(contant.type_all_form[4]+contant.type_detail_form[4]+id);
                drawingService.insert(item1);
            }
        }
        if(t.getCameraNumber()!=null&& t.getCameraNumber().size()>0){
            for (int i = 0; i <t.getCameraNumber().size() ; i++) {
                CameraEntity item = t.getCameraNumber().get(i);
                item.setPosterId(user.getUserId());
                item.setPostTime(data.getCreateAt());
                item.setCreateUser(user.getUsername());
                item.setProjectId(data.getProjectId());
                item.setTypeWhere(2);
                item.setTypeForm(101);
                item.setDataId(id);
                item.setFiledName("0");
                item.setNumber(contant.type_all_form[4]+contant.type_detail_form[4]+id);
                cameraService.insert(item);
            }
        }
        if(t.getVideoNumber()!=null&& t.getVideoNumber().size()>0){
            vedioService.delete(new EntityWrapper<VedioEntity>().eq("data_id",id).eq("type_form",101));
            for (int i = 0; i <t.getVideoNumber().size() ; i++) {
                VedioEntity item3 = t.getVideoNumber().get(i);
                item3.setPosterId(user.getUserId());
                item3.setPostTime(data.getCreateAt());
                item3.setCreateUser(user.getUsername());
                item3.setProjectId(data.getProjectId());
                item3.setTypeWhere(2);
                item3.setTypeForm(101);
                item3.setDataId(id);
                item3.setPlateNumber(contant.type_all_form[4]+contant.type_detail_form[4]+id);
                vedioService.insert(item3);
            }
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public DiscoverForm getDiscoverDetail(long id) {
        DiscoverForm data = new DiscoverForm();
    DiscoverEntity t = new DiscoverEntity();
    t.setId(id);
    t = this.baseMapper.selectById(t);
        List<ConnectionsEntity> collections = this.baseMapper.selectCollecton(id);
        List<TestEntity> tests = this.baseMapper.selectTest(id);
        StringBuffer cc = new StringBuffer();
        List<CameraEntity> ccs = cameraService.selectList(new EntityWrapper<CameraEntity>().eq("filed_name","4").eq("data_id",id).eq("type_form",101));
        for (int i = 0; i <ccs.size() ; i++) {
            if(i==0){
                cc.append(""+ccs.get(i).getUrl());
            }else{
                cc.append(","+ccs.get(i).getUrl());
            }
        }
        List<DrawingEntity> dds = drawingService.selectList(new EntityWrapper<DrawingEntity>().eq("data_id",id).eq("type_form",101));
        List<CameraEntity> cc1s = cameraService.selectList(new EntityWrapper<CameraEntity>().eq("filed_name","0").eq("data_id",id).eq("type_form",101));
        List<VedioEntity> vvs = vedioService.selectList(new EntityWrapper<VedioEntity>().eq("data_id",id).eq("type_form",101));
        data.setSchematic(ccs);
        data.setDrawingNumber(dds);
        data.setCameraNumber(cc1s);
        data.setVideoNumber(vvs);
        data.setDiscoverEntity(t);
        data.setConnectionsEntities(collections);
        data.setTestEntities(tests);
        return data;
    }

    @Override
    @Transactional(rollbackFor=Exception.class)
    public void deleteDiscover(long id) {
        drawingService.delete(new EntityWrapper<DrawingEntity>().eq("data_id",id).eq("type_form",101));
        cameraService.delete(new EntityWrapper<CameraEntity>().eq("filed_name","4").eq("data_id",id).eq("type_form",101));
        vedioService.delete(new EntityWrapper<VedioEntity>().eq("data_id",id).eq("type_form",101));
        this.baseMapper.deleteById(id);
        checkService.deleteMe(5,101,id);
        collectionService.delete(new EntityWrapper<ConnectionsEntity>().eq("discover_id",id));
        testService.delete(new EntityWrapper<TestEntity>().eq("discover_id",id));
    }
    @Override
    public List<Map<String,Object>> selectReginal(Map map) {
        return this.baseMapper.selectReginal(map);
    }

    @Override
    public int selectCountP(Map map) {
        return this.baseMapper.selectCountP(map);
    }
}
